﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout2.cshtml";
    var lst = ViewData["lstmes"] as List<SmallJob.Model.MConversation>;
    //var provides = ViewData["provides"] as List<SmallJob.Model.Provide>;
    SmallJob.Model.MConversation first = null;
    var userid = (int)ViewData["curuserid"];
    var isProvideFirst = true;
    if (lst.Count > 0)
    {
        first = lst.First();
        isProvideFirst = (first.Type == (int)SmallShop.Services.Implements.MessageType.PROVIDE);
    }
}
<div class="inbox-detail">
    <div class="inbox-detail-container">
        <div class="row-fluid">
            <div class="span4 sidebar">
                <div class="inbox-detail-box sidebar-block" id="service-detail-creator">
                    <div class="container-header" id="navigate-cvs">
                        <ul class="nav nav-pills inbox-nav">
                            <li class="@(isProvideFirst ? "active" : "")" data-name="provide">
                                <a href="#" id="provide-link-all">Dịch vụ</a>
                            </li>
                            <li class="@(isProvideFirst ? "" : "active")" data-name="request">
                                <a href="#" id="order-link-all">Yêu cầu</a>
                            </li>
                        </ul>
                    </div>
                    <div class="conversation-list">
                        @foreach (var item in lst)
                        {
                            var client = (item.Person1ID == userid ? item.User2 : item.User1);
@*<a class="conversation-item @(lst.IndexOf(item) == 0 ? "conversation-item-active" : "")" id="Cvs-@item.Message.ID" style="@(isProvideFirst ? (item.Order != null ? "" : "display:none;") : (item.Order != null ? "display:none;" : ""))" onclick="loadCvs(this);" href="#">*@
                            <a class="conversation-item @(lst.IndexOf(item) == 0 ? "conversation-item-active" : "")" style="@(isProvideFirst ? (item.Provide == null ? "display:none;" : "") : (item.Request == null ? "display:none;" : ""))" id="Cvs-@item.ShortID" onclick="loadCvs(this);" href="#">
                                <input type="hidden" value="@item.Type"  class="hidden-type-cvs"/>
                                <div class="conversation-item-inner">
                                    <div class="sender-avatar pull-left" title="Thăm trang cá nhân người này.">

                                        <img src="@(string.IsNullOrWhiteSpace(client.Avatar) ? Url.Content("~/Content/img/blank-avatar.jpg") : client.Avatar)?w=64&h=64&q=90&format=jpg&mode=crop" />
                                    </div>
                                    <div class="inbox-body">
                                        <p class="inbox-meta">
                                            <span class="inbox-sender">@client.Name</span>
                                            <span class="inbox-date">@string.Format("{0:m}", item.Message.Date)</span>
                                        </p>
                                        <p class="inbox-title">
                                            @(item.Request == null ? (item.Provide == null ? "Private" : item.Provide.Title) : item.Request.Title)
                                        </p>
                                    </div>
                                </div>
                            </a>
                        }
                    </div>
                    <p class="text-center" style="margin-top:10px;"><a href="javascript:void(0)" onclick="loadmoreconversation(this)" data-nextprovide="@ViewData["nextpageprovide"]" data-nextrequest="@ViewData["nextpagerequest"]" data-nextprivate="2" id="cvs-load-more">Xem thêm</a></p>
                </div>
            </div>
            <div class="span8 inbox-detail-box main-column">
                <div id="is-page-more"><a data-nextpage="0" id="get-next-page" onclick="loadmoremessage(this)">Lấy thêm tin nhắn cũ.</a></div>
                <div class="conversation-detail-inner" id="conversation-container-message">
                    @* @{if (first != null) { Html.RenderPartial("ConversationPV", first); }}*@
                </div>
                <div class="reply-box">
                    <input type="hidden" id="reply-cvs-id" value="@(first == null ? "0" : first.ShortID)" />
                    <form class="form-horizontal reply-form">
                        <textarea rows="3" class="span12"></textarea>
                        <button type="submit" class="btn btn-primary conversation-send" id="reply-btn">Gửi</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>

    function loadCvs(that) {
        var id = $(that).attr('id').substring('Cvs-'.length);;
        $.ajax({
            type: "POST",
            url: '/Personal/ConversationPV',
            data: { cvsId: id },
            success: function (data) {
                var pagemore = $(data).filter('#new-next-page');
                if (pagemore.length !== 0) {
                    if (!$('#get-next-page').is(":visible")) {
                        $('#get-next-page').show();
                    }
                    $('#get-next-page').attr('data-nextpage', $(pagemore).attr('data-nextpage'));
                } else {
                    $('#get-next-page').hide();
                }
                $('#conversation-container-message').html($(data).filter('.wrapper-all-message').html());
                $('.conversation-item-active').removeClass('conversation-item-active');
                $(that).addClass('conversation-item-active');
                $('#reply-cvs-id').val(id);
                $.post('@Url.Action("GetUnread", "Message")', function (data) {
                    if (data >= 0) {
                        $('#unread-mess').text(data);
                    }
                });
                $('#conversation-container-message').animate({ scrollTop: $('#conversation-container-message')[0].scrollHeight }, 1000);
            }
        });
    }
    function activeorder() {
        $('.hidden-type-cvs').each(function () {
            var type = $(this).val();
            if (type == '@((int)SmallShop.Services.Implements.MessageType.REQUEST)') {
                $(this).parent().css('display', '');
            } else {
                $(this).parent().css('display', 'none');
            }
        });
        $('#provide-link-all').parent('li').removeClass('active');
        $('#order-link-all').parent('li').addClass('active');
        checkandenablecvsmore();
    }
    function activeprovide() {
        $('.hidden-type-cvs').each(function () {
            var type = $(this).val();
            if (type == '@((int)SmallShop.Services.Implements.MessageType.PROVIDE)') {
                $(this).parent().css('display', '');
            } else {
                $(this).parent().css('display', 'none');
            }
        });
        $('#provide-link-all').parent('li').addClass('active');
        $('#order-link-all').parent('li').removeClass('active');
        checkandenablecvsmore();
    }
    $('#order-link-all').click(function () {
        activeorder();
    });
    $('#provide-link-all').click(function () {
        activeprovide();
    });
    $('#reply-btn').click(function (e) {
        e.preventDefault();
        if ($('#reply-cvs-id').val() <= 0) return;
        $('#reply-btn').attr('disabled', 'disabled');
        var mes = $('.reply-box').find('textarea').val();
        if (mes.length > 0) {
            var preId = $('#reply-cvs-id').val();
            $.post("/Personal/SendMessage", { mes: mes, preId: preId }, function (data) {
                $('#conversation-container-message').append($(data).filter('.wrapper-all-message').html());
                $('.reply-box').find('textarea').val("");
                $('#conversation-container-message').animate({ scrollTop: $('#conversation-container-message')[0].scrollHeight }, 1000);
                $('#reply-btn').removeAttr('disabled');
            });
        }

    });
    $('#conversation-container-message').animate({ scrollTop: $('#conversation-container-message')[0].scrollHeight }, 1000);
    $('document').ready(function () {
        var cvstoload = null;
        if (window.location.hash == "#hoi-dap-yeu-cau") {
            activeorder();
            cvstoload = $('.conversation-item').filter(':visible').first();
        }
        else if (window.location.hash == "#hoi-dap") {
            activeprovide();
            cvstoload = $('.conversation-item').filter(':visible').first();
        } else {
            cvstoload = $('#Cvs-' + window.location.hash.substring(1));
            if (cvstoload != null && cvstoload.length !== 0) {
                var typecvs = $(cvstoload).find('.hidden-type-cvs');
                var type = $(typecvs).val();
                if (type == '@((int)SmallShop.Services.Implements.MessageType.REQUEST)') {
                    activeorder();
                } else {
                    activeprovide();
                }
            }
        }
        if (cvstoload == null || cvstoload.length === 0) {
            cvstoload = $('.conversation-item').filter(':visible').first();
        }
        loadCvs(cvstoload);
        checkandenablecvsmore();
    });
function loadmoremessage(that) {
    if (isdisablemore(that)) {
        return;
    }
    disablemore(that);
    var nextpage = $(that).attr('data-nextpage');
    var cvsid = $('#reply-cvs-id').val();
    $.ajax({
        type: "POST",
        url: '/Personal/ConversationPV',
        data: { pageforload: nextpage, cvsId: cvsid },
        success: function (data) {
            var pagemore = $(data).filter('#new-next-page');
            if (pagemore.length !== 0) {
                if (!$('#get-next-page').is(":visible")) {
                    $('#get-next-page').show();
                }
                $('#get-next-page').attr('data-nextpage', $(pagemore).attr('data-nextpage'));
            } else {
                $('#get-next-page').hide();
            }
            $('#conversation-container-message').prepend($(data).filter('.wrapper-all-message').html());

            $.post('@Url.Action("GetUnread", "Message")', function (data) {
                if (data >= 0) {
                    $('#unread-mess').text(data);
                }
            });
            enablemore(that);
        }
    });
}
function loadmoreconversation(that) {
    if (isdisablemore(that)) {
        return;
    }
    disablemore(that);
    var liactive = $('#navigate-cvs').find('li.active');
    var curactive = $(liactive).attr('data-name');
    if (curactive == "provide") {
        var nextpage = $(that).attr('data-nextprovide');
        $.post("/Personal/LoadMoreCvsPV", { type: '@((int)SmallShop.Services.Implements.MessageType.PROVIDE)', page: nextpage }, function (data) {
            var newcvs = $(data).filter('.wrapper-conversations');
            $('.conversation-list').append(newcvs);
            var pagemore = $(data).filter('#new-next-page');
            if (pagemore.length !== 0) {
                $(that).attr('data-nextprovide', $(pagemore).attr('data-nextpage'));
            } else {
                $(that).attr('data-nextprovide', 0);
            }
            enablemore(that);
            checkandenablecvsmore();
        });
    }
    if (curactive == "request") {
        var nextpage = $(that).attr('data-nextrequest');
        $.post("/Personal/LoadMoreCvsPV", { type: '@((int)SmallShop.Services.Implements.MessageType.REQUEST)', page: nextpage }, function (data) {
            var newcvs = $(data).filter('.wrapper-conversations');
            $('.conversation-list').append(newcvs);
            var pagemore = $(data).filter('#new-next-page');
            if (pagemore.length !== 0) {
                $(that).attr('data-nextrequest', $(pagemore).attr('data-nextpage'));
            } else {
                $(that).attr('data-nextrequest', 0);
            }
            enablemore(that);
            checkandenablecvsmore();
        });
    }
}
function checkandenablecvsmore() {
    var liactive = $('#navigate-cvs').find('li.active');
    var curactive = $(liactive).attr('data-name');
    if (curactive == "provide") {
        var nextpage = $('#cvs-load-more').attr('data-nextprovide');
        if (nextpage == 0) {
            $('#cvs-load-more').hide();
        } else {
            $('#cvs-load-more').show();
        }
    }
    if (curactive == "request") {
        var nextpage = $('#cvs-load-more').attr('data-nextrequest');
        if (nextpage == 0) {
            $('#cvs-load-more').hide();
        } else {
            $('#cvs-load-more').show();
        }
    }
}
function disablemore(that) {
    $(that).addClass('moredisable');
}
function enablemore(that) {
    $(that).removeClass('moredisable');
}
function isdisablemore(that) {
    return $(that).hasClass('moredisable');
}
</script>
